In [1]:
 
In [2]:
import pandas as pd
pd.options.mode.chained_assignment = None

import numpy as np
import altair as alt

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:98% !important; }</style>"))

from IPython.display import display, HTML

display(HTML("""
<style>
form.vega-bindings {
  position: absolute;
  right: 900px;
  top: 1000px;
}

</style>
"""))


des3_periods_v2 = pd.read_csv('des3_periods_v2.csv', index_col=0)
des3_df = pd.read_csv('des3_df_barplot.csv', index_col=0)
map_df2 = pd.read_csv('map_df2.csv', index_col=0)

alt.data_transformers.disable_max_rows()

threshold = pd.DataFrame([{"threshold": 1000}])
selector = alt.selection_multi(fields=['Country'])
selector2 = alt.selection_multi(fields=['Country'])

##############
## BAR PLOT ##
##############
bars = alt.Chart(des3_df).mark_bar().encode(
    x=alt.X('sum(anc_places):Q', title='Sum of Ancient Places'),
    y=alt.Y("Country:N", title='Country',
            sort=alt.Sort(field="anc_places", order="descending"),
            ),
   # color=alt.condition(selector,
   #                     'anc_places:O',
   #                     alt.value('lightgray'),
   #                     legend=None
   #                    )
).properties(
    title="No. Ancient Places per Country (Top 30)",
    #fontSize=20,
    width=530,
    height=720
).transform_calculate(
    "baseline", "90"
).add_selection(
    selector
)

text = alt.Chart(des3_df).mark_text(dx=20, dy=0, color='black').encode(
    x=alt.X('sum(anc_places):Q', stack='zero'),
    y=alt.Y('Country:N', sort=alt.Sort(field="anc_places", order="descending")),
   # detail=feattt,#'site:N',
    text=alt.Text('sum(anc_places):Q', format='.0f')
)

highlight = alt.Chart(des3_df).mark_bar(color="#e45755").encode(
    x=alt.X('sum(anc_places):Q'),
    x2= alt.X2('baseline:Q'),
    y=alt.Y('Country:N', sort=alt.Sort(field="anc_places", order="descending")),
).transform_calculate(
    "baseline", "1000"
).transform_window(
    rank='rank(anc_places)',
    sort=[alt.SortField("anc_places", order="descending")],
).transform_filter(
    alt.datum.anc_places > 1000
).transform_filter(
    selector #brush
)

rule = alt.Chart(threshold).mark_rule().encode(
    x='threshold:Q'
)

barss = bars + text + highlight + rule

##################
## Time Periods ##
##################
color_scale = alt.Scale(
    domain=[
            'Paleolithic: [-2600000, -18000]',
            'Mesolithic: [-18000, -9000]',
            'Neolithic: [-9000, -5000]',
            'Chalcolithic: [-5000, -2500]',
            'Bronze: [-2500, -1000]',
            'Archaic: [-1000, -550]',
            'Classical: [-550, -330]',
            'Hellenistic-Republican: [-330, -30]',
            'Roman: [-30, 300]',
            'Late Antique: [300, 640]',
            'Medieval/Byzantine: [640, 1500]',
            'Modern Era: [1500, 2100]'
    ],
    range=["#A40126", "#D73027", "#F56D43", "#FDAE61", "#FFE090", "#FFFFBF", "#E0F3F9", "#ABD9E9", "#75ADD2", "#4575B5", "#3145ce", "#314553"]
)

bubbles = alt.Chart(des3_periods_v2).mark_circle(
    opacity=0.8,
    stroke='black',
    strokeWidth=1
).encode(
    alt.X('avg_year:O', axis=alt.Axis(labelAngle=0), title='Time'),
    alt.Y('Country:N', axis=alt.Axis(labelAngle=0, grid=True), title='Countries'),
    alt.Size('count(Country):Q',
        scale=alt.Scale(range=[0, 4000]),
        legend=alt.Legend(title='No. Ancient Places')
        ),
   # color=alt.condition(selector, #brush,
   #                     'info_legend:N',
   #                     alt.value('lightgray'),
   #                     legend=alt.Legend( title='Time Periods'),
   #                     scale=color_scale
   #                    ),
    #alt.Color('periods:N')
    color=alt.Color(
        'info_legend:N',
        legend=alt.Legend( title='Time Periods'),
        scale=color_scale,
        ),
    tooltip = ['Country', 'info_legend', 'count()']
).properties(
    title= "Countries' No. Ancient Places through Time Periods (Top 30)",
    width=1200,
    height=720
).add_selection(
    selector
).transform_filter(
    selector #brush
)

#################
##  WORLD MAP  ##
#################
from vega_datasets import data
from altair.expr import datum

countries = alt.topo_feature(data.world_110m.url, 'countries')

'''
slider_start = alt.binding_range(min=-5000,#map_df2.minDate.min(),
                                 max=map_df2.maxDate.max(),
                                 step = 20)

slider_end = alt.binding_range(min=-5000,#map_df2.minDate.min(),
                                 max=map_df2.maxDate.max(),
                                 step = 10)

select_year_start = alt.selection_single(name='minDate',
                                        fields=['minDate'],
                                        bind=slider_start, init={'minDate': -5000})

select_year_end = alt.selection_single(name='maxDate',
                                        fields=['maxDate'],
                                        bind=slider_end, init={'maxDate': 2100})
'''

slider = alt.binding_range(min=-19000,#map_df2.minDate.min(),
                           max=map_df2.maxDate.max(),
                           step = 50)

select_year = alt.selection_single(name='minDate',
                                    fields=['minDate'],
                                    bind=slider, init={'minDate': -19000 })#map_df2.minDate.min()})






world_map = alt.Chart(countries).mark_geoshape(
    fill='lightgray',
    stroke='white',
).project(
    type= 'mercator',
    scale= 450,                          # Magnify
    center= [-35.3,62.7],                     # [lon, lat]
    clipExtent= [[0, 0], [1300, 800]],    # [[left, top], [right, bottom]]
).properties(
    title='Ancient Places Locations throughout History',
    width=1300, height=800
)


points = alt.Chart(map_df2).transform_filter(
 # (datum.minDate <= select_year_start.minDate) & (datum.maxDate >= select_year_end.maxDate)
    (datum.minDate <= select_year.minDate) & (datum.maxDate >= select_year.minDate)
).add_selection(
  #  select_year_end,
  #  select_year_start,
    select_year
).mark_point(size=1).encode(
    latitude="reprLat:Q",
    longitude="reprLong:Q",
    tooltip=['title']
)




row2 = world_map + points

row1 = alt.hconcat(barss, bubbles, spacing=20)

#barss | bubbles
#bubbles

dashboard2 = alt.vconcat(row1, row2, spacing=20,padding={"top":75}, title={"text":"Locations of Ancient Places through Historic Time Periods",
                                                   "fontSize":23,
                                                   "anchor":"middle", 
                                                   "baseline":"alphabetic"}).configure_axis(
    labelFontSize=13,
    titleFontSize=15
).configure_legend(
titleFontSize=18,
labelFontSize=15,
labelLimit= 0
)
dashboard2
Out[2]:
In [ ]:
 
In [ ]:
 
In [ ]: